$(document).ready(function(){
	var count=0;
	var sum=0;
	var numb=0;
		
	$.ajax({
		type:"get",
		url:"../js/storeData.json",
		async:true,
		dataType:"json",
		success:function(data){
			for (key in data) {
				var str='<div class="accordion-content"><div>';
				$(".content").append(str);
//				detail(data[key],numb++);
			}
			//页面初始化
			$(".accordion-content").eq(0).css("display","block").siblings(".accordion-content").css("display","none");
			
			//点击左侧分类列表
			mui("body").on("tap",".submenu2 li",function(){
				$(this).css("border-left","4px solid red").siblings().css("border-left","0");
				$(".accordion-content").eq($(this).index()).css("display","block").siblings(".accordion-content").css("display","none");
			})
		}
	});
	
	//渲染函数
//	function detail(classify,num){
//		var str="";
//		for (var i=0;i<classify.length;i++) {
//			str+='<div class="list"><div class="commodity" style="float:left;margin-left:5px;font-size:14px;">'+classify[i].commodity+'<p>￥<span id="price" class="price" style="color:green;font-size:14px;">'+classify[i].price+'</span><span>/位</span></p></div><div class="mui-numbox" id="numbox_demo1" data-numbox-step="1" data-numbox-min="0" data-numbox-max="1000" data-goodsNum="'+classify[i].commoduityId+'" data-goodsType="'+classify[i].goodsType+'"><button class="mui-btn mui-numbox-btn-minus" id="btnDelete" type="button" style="color:red;">-</button><input class="mui-numbox-input count" type="number" value="0" data-num="0" disabled="disabled" /><button class="mui-btn mui-btn-numbox-plus" id="btnAdd" type="button" style="color:red;">+</button></div><div style="float:right;margin-top:10px;margin-right:20px;"></div></div>'
//		}
//		$(".accordion-content").eq(num).append(str);
		goods();
//	}
	
	//点击物品减少键
	mui("body").on("tap",".mui-numbox-btn-minus",function(){
		var self=$(this);
		var num=Number($(this).siblings("input").val());
		var price=Number($(this).parent().siblings(".commodity").find(".price").html());
		if (num>0) {
			$(".countPrice").val("￥"+(Number($(".countPrice").val().substring(1))-price).toFixed(2));
			$(this).siblings("input").val(--num);
			$("#sum").html(--sum);
			if (sum==0) {
				$("#sum").css("display","none");
			}
			var index=Number(self.parent().attr("data-goodsType"));
			var goodsNum=$(".goods").eq(index-1).html()*1-1;
			$(".goods").eq(index-1).html(goodsNum);
		}
		localStorage.setItem("price",$(".countPrice").val().substring(1));
		goods();
	})
	
	//点击物品增加键
	mui("body").on("tap",".mui-btn-numbox-plus",function(){
		var self=$(this);
		var num=Number($(this).siblings("input").val());
		var price=Number($(this).parent().siblings(".commodity").find(".price").html());
		$(".countPrice").val("￥"+(Number($(".countPrice").val().substring(1))+price).toFixed(2));
		$(this).siblings("input").val(++num);
		$("#sum").html(++sum);
		$("#sum").css("display","block");
		localStorage.setItem("price",$(".countPrice").val().substring(1));
		var index=Number(self.parent().attr("data-goodsType"));
		var goodsNum=$(".goods").eq(index-1).html()*1+1;
		$(".goods").eq(index-1).html(goodsNum);
		goods();
	})
	
	function goods(){
		for (var i=0;i<$(".goods").length;i++) {
			if ($(".goods").eq(i).html()==0) {
				$(".goods").eq(i).css("display","none");
			} else{
				$(".goods").eq(i).css("display","block");
			}
		}
	}
	
	//点击“去收款”按钮
	mui("body").on('tap',".goPay",function(){
		if (Number($(".countPrice").val().substring(1))>0) {
			//判断网络是否错误
			if (window.navigator.onLine==true) {
				mui.openWindow('../view/public/barcode_scan.html',"barcode_scan");
			} else{
				mui.toast("网络错误，请检查您的网络",{ duration:'long', type:'div' });
			}
		} else{
			mui.alert("请选择商品","")
		}
	});
	
})